//
//  EmergenceyContactView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/23.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class EmergenceyContactView: UIView {
    
    private var titleLabels = [UILabel]()
    private var inputTexts = [InputTextView]()
    private let titles = ["姓名","手机号","邮箱地址"]
    // 添加联系人
    private let addContactBtn = UIButton()
    // 乘机人
    private let passengerText = UILabel(title: "联系人信息",color: UIColor.black)
    
    // 回调
    public var complete: (() -> Void)?
    
    public var accountBean:AccountBean? {
        didSet {
            setNeedsLayout()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        init_views()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        // 乘机人
        passengerText.frame = CGRect(x: MARGIN, y: MARGIN, width: MARGIN100, height: MARGIN)
        passengerText.font = UIFont.boldSystemFont(ofSize: 17)
        addSubview(passengerText)
        // 宽度
        let w = frame.width - MARGIN * 2
        // 姓名
        let username = titleLabels[0]
        username.frame = CGRect(x: MARGIN, y: passengerText.frame.maxY + MARGIN30, width: MARGIN50, height: MARGIN)
        // 姓名输入
        let usernameInput = inputTexts[0]
        usernameInput.text = "请填入"
        usernameInput.frame = CGRect(x: MARGIN, y: username.frame.maxY, width: w, height: MARGIN50)
        // 添加联系人
        addContactBtn.layer.borderWidth = 1
        addContactBtn.layer.cornerRadius = 4
        addContactBtn.setTitleColor(COLOR_NICE, for: .normal)
        addContactBtn.layer.borderColor = UIColor.gray.cgColor
        addContactBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        let contactBtnY = usernameInput.frame.maxY - MARGIN40
        addContactBtn.frame = CGRect(x: frame.width - MARGIN90, y: contactBtnY, width: MARGIN70, height: MARGIN30)
        addSubview(addContactBtn)
        // 添加乘机人 点击事件
        addContactBtn.addTarget(self, action: #selector(clickItem), for: .touchUpInside)
        // 手机号
        let mobile = titleLabels[1]
        mobile.frame = CGRect(x: MARGIN, y: usernameInput.frame.maxY + MARGIN20, width: MARGIN50, height: MARGIN)
        // 手机号输入
        let mobileInput = inputTexts[1]
        mobileInput.text = "请填入"
        mobileInput.frame = CGRect(x: MARGIN, y: mobile.frame.maxY, width: w, height: MARGIN50)
        // 邮箱
        let email = titleLabels[2]
        email.frame = CGRect(x: MARGIN, y: mobileInput.frame.maxY + MARGIN20, width: MARGIN50, height: MARGIN)
        // 邮箱输入
        let emailInput = inputTexts[2]
        emailInput.isHaveLine = false
        emailInput.text = "请填入"
        emailInput.frame = CGRect(x: MARGIN, y: email.frame.maxY, width: w, height: MARGIN50)
        // 赋值
        if accountBean != nil {
            emailInput.text = accountBean?.email
            mobileInput.text = accountBean?.mobile
            usernameInput.text = accountBean?.username
        }
    }
    
    
    @objc func clickItem() -> Void {
        if let callback = complete {
            callback()
        }
    }
    
    private func init_views() -> Void {
        for i in 0...2 {
            // 标题
            let label = UILabel()
            label.text = titles[i]
            label.textColor = COLOR_NICE
            label.font = UIFont.systemFont(ofSize: 12)
            titleLabels.append(label)
            // 输入
            let input = InputTextView()
            input.textColor = COLOR_NICE
            input.isEnabled = false
            input.font = UIFont.systemFont(ofSize: 16)
            inputTexts.append(input)
            // 添加到父控件
            addSubview(label)
            addSubview(input)
        }
        
        addContactBtn.setTitle("+ 联系人", for: .normal)
    }
}
